<?php
namespace jy_yiliao;
defined('BY_JYA') or exit('error');
/*
	科室
*/
class lesson extends \plugin{
	/*
		获取最近科室
	*/
	public function get_lessonlast($body=array()){
		$class=pdo_get('jy_yiliao_class',array('id'=>$body['classid']));
		$class['class_lesson']=json_decode($class['class_lesson'],true);
		$weekid=date("w");
		$ymd=date('Y-m-d',TIMESTAMP);
		paixu($class['class_lesson'],'xinqi','asc');
		foreach($class['class_lesson'] as $item){
			if($item['xinqi']==0 || $item['xinqi']>=$weekid){
					$item['shijian']=strtotime($ymd.' '.$item['time']['start']);
					if($item['shijian']>TIMESTAMP){
						$lessontitle=pdo_getcolumn('jy_yiliao_lesson',array('id'=>$item['lessonid']),'title');
						$lessontime=$this->week_days[$item['xinqi']].'~'.$item['time']['start'];
						break;
					}
			}
		}
		if($lessontitle){
			$lessonlast='《'.$lessontitle.'》'.$lessontime;
		}else{
			$lessonlast='本周没有任何科室';
		}
		return $lessonlast;
	}
	public function get_days($classid,$days=7){
		$class=pdo_get('jy_yiliao_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$classid));
		//日期
		$data=array();
		$weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
		$days=$days>0?$days:7;
		for($i=0;$i<$days;$i++){
			$data[]=array(
				'ymd'=>date("Y-m-d",strtotime("+".$i." day")),
				'riqi'=>date("m-d",strtotime("+".$i." day")),
				'xinq'=>"星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))))],
			);
		}
		return $data;
	}
	public function get_uptime($ontime,$isarray=false){
		$hours=date('H',$ontime);
		$weekid=date("w",$ontime);
		$week=$this->week_days[$weekid];
		$ontimeinfo='【'.$week.'】'.date('Y-m-d',$ontime);
		if($hours>12){
				$ontimeinfo .=' [下午]';
		}else{
				$ontimeinfo .=' [上午]';
		}
		if($isarray){
			$res=array(
				'hours'=>$hours,
				'weekid'=>$weekid,
				'days'=>date('Y-m-d',$ontime),
				'ontimeinfo'=>$ontimeinfo
			);
			return $res;
		}else{
			return $ontimeinfo;
		}
	}
	public function getserve_time($ontime,$daytime=''){
			$timestamp = array();
			$daydata=date('Y-m-d',$daytime);
			if($ontime['start']){
				$start=$daydata.' '.$ontime['start'];
				$start=strtotime($start);
			}
			if($ontime['end']){
				$end=$daydata.' '.$ontime['end'];
				$end=strtotime($end);
			}
			$basic=$this->plugin_setting('basic');
			$time_yue=$basic['time_yue']*60?:900;
			//print_r($end.'~~'.$start);exit;
			$times=array();
			for($i = $start; $i < $end;) {
					$times[] = array(
						'start' => date('H:i', $i),
						'end' => date('H:i', $i + $time_yue),
						'timestamp' => $i + $time_yue,
						'timev' =>date('H:i', $i).'~'.date('H:i', $i + $time_yue),
					);
					$i += $time_yue;
			}
			return $times;
	}
	public function add_lesson_log($params,$uid=''){
		$isadd=$this->pdo_get('lesson_log',array('day'=>$params['daydata'],'lesson_id'=>intval($params['lesson_id']),'mid'=>$params['mid']?:$uid,'uniacid'=>$_SESSION['uniacid']));
		if($isadd['id']){
			exi('您已预约该科室！','error','/yuyue_order/');
		}
		$uid=$uid?$uid:$_SESSION['uid'];
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'logsn'=>get_ordersn($this->tablename('lesson_log'),'logsn'),
			'mid'=>$params['mid']?:$uid,
			'storeid'=>intval($params['storeid']),
			'lesson_id'=>intval($params['lesson_id']),
			'day'=>$params['daydata'],
			'worker_id'=>intval($params['worker_id']),
			'money'=>$params['money'],
			'remark'=>$params['remark'],
			'daydata'=>$params['daydata'],
			'ontimestart'=>$params['ontimestart'],
			'createtime'=>TIMESTAMP,
		);
		$this->pdo_insert('lesson_log',$order);
		$log_id=$this->pdo_insertid();
		return $log_id;
	}
	public function get_months($workerid=0,$storeid=0){
		$year = date('Y');
		$month = date('m');
		$list = array();
		$i = $month;
		while ($i <= 12) {
			$list[] = array(
				'title'=>$year.'年'.$i,
				'year'=>$year,
				'month'=>$i
			);
			++$i;
		}
		$store=pdo_getcolumn('jy_yiliao_store',array('id'=>$storeid),'title');
		$worker=pdo_get('jy_yiliao_worker',array('id'=>$workerid));
		$worker['paibans']=json_decode($worker['paibans'],true);
		foreach($list as &$item){
			$day = $this->getcalendar($item['month'],$year);
			if(!$day){
				exi('请先安装Calendar扩展，获取日历信息！','error');
			}
			$startime=strtotime($year.'-'.$item['month']);
			$endtime=strtotime($year.'-'.$item['month'].'-'.$day);
			$nowtime=array($startime,$endtime);
			$calendar=pdo_getall('jy_yiliao_lesson_pai',array('workerid'=>$workerid,'ontime between'=>$nowtime,'uniacid'=>$_SESSION['uniacid']),array('storeid','time_hour','onday'),'onday');
			$item['daynum']=$day;
			$days = array();
			$indays = array();
			$i = 1;
			$lid = 0;
			$item['startweekid']=date("w",strtotime($year.'-'.$item['month'].'-01'));
			for($bui=0;$bui<$item['startweekid'];$bui++){
				$indays[]=array(
					'ispass'=>true,
				);
			}
			while ($i <= $day){
					if ($i < 10) {
						$i = '0' . $i;
					}
					$daydata=$year.'-'.$item['month'].'-'.$i;
					$weekid=date("w",strtotime($daydata));
					if(!$weekid && $i>0){
						$indays = array();
						$lid++;
					}
					if(strtotime($daydata)+20*3600<time()){
						$ispass=true;
					}else{
						$ispass=false;
					}
					if (isset($calendar[$daydata])){
						$store=pdo_getcolumn('jy_yiliao_store',array('id'=>$calendar[$daydata]['storeid']),'title');
						$isworker=true;
					}else{
						$isworker=false;
					}
					$indays[]=array(
						'day'=>$i,
						'daydata'=>$daydata,
						'ispass'=>$ispass,
						'isworker'=>$isworker,
						'noshow'=>$noshow?:false,
						'store'=>$store?:'',
						'isman'=>$isman?:false,
					);
					$days[$lid]=array('indays'=>$indays);
					++$i;
			}
			$item['days']=$days;
		}
		return $list;
	}
	public function getcalendar($month='',$year='')
	{
		$day = date("t",strtotime($year.'-'.$month));
		return $day;
	}
}